﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MALRECrewrite.Etc
{
    public class HTMLCharacterConverter
    {
        private static Dictionary<char, string> criticals = new Dictionary<char, string>();

        private static void CreateCrits()
        {
            criticals.Add('"', "&quot;");
            criticals.Add('>', "&lt;");
            criticals.Add('<', "&gt;");
            criticals.Add('¡', "&iexcl;");
            criticals.Add('¢', "&cent;");
            criticals.Add('£', "&pound;");
            criticals.Add('¤', "&curren;");
            criticals.Add('¥', "&yen;");
            criticals.Add('¦', "&brvbar;");
            criticals.Add('§', "&sect;");
            criticals.Add('¨', "&uml;");
            criticals.Add('©', "&copy;");
            criticals.Add('ª', "&ordf;");
            criticals.Add('«', "&laquo;");
            criticals.Add('¬', "&not;");
            criticals.Add('­', "&shy;");
            criticals.Add('®', "&reg;");
            criticals.Add('¯', "&macr;");
            criticals.Add('°', "&deg;");
            criticals.Add('±', "&plusmn;");
            criticals.Add('²', "&sup2;");
            criticals.Add('³', "&sup3;");
            criticals.Add('´', "&acute;");
            criticals.Add('µ', "&micro;");
            criticals.Add('¶', "&para;");
            criticals.Add('·', "&middot;");
            criticals.Add('¸', "&cedil;");
            criticals.Add('¹', "&sup1;");
            criticals.Add('º', "&ordm;");
            criticals.Add('»', "&raquo;");
            criticals.Add('¼', "&frac14;");
            criticals.Add('½', "&frac12;");
            criticals.Add('¾', "&frac34;");
            criticals.Add('¿', "&iquest;");
            criticals.Add('À', "&Agrave;");
            criticals.Add('Á', "&Aacute;");
            criticals.Add('Â', "&Acirc;");
            criticals.Add('Ã', "&Atilde;");
            criticals.Add('Ä', "&Auml;");
            criticals.Add('Å', "&Aring;");
            criticals.Add('Æ', "&AElig;");
            criticals.Add('Ç', "&Ccedil;");
            criticals.Add('È', "&Egrave;");
            criticals.Add('É', "&Eacute;");
            criticals.Add('Ê', "&Ecirc;");
            criticals.Add('Ë', "&Euml;");
            criticals.Add('Ì', "&Igrave;");
            criticals.Add('Í', "&Iacute;");
            criticals.Add('Î', "&Icirc;");
            criticals.Add('Ï', "&Iuml;");
            criticals.Add('Ð', "&ETH;");
            criticals.Add('Ñ', "&Ntilde;");
            criticals.Add('Ò', "&Ograve;");
            criticals.Add('Ó', "&Oacute;");
            criticals.Add('Ô', "&Ocirc;");
            criticals.Add('Õ', "&Otilde;");
            criticals.Add('Ö', "&Ouml;");
            criticals.Add('×', "&times;");
            criticals.Add('Ø', "&Oslash;");
            criticals.Add('Ù', "&Ugrave;");
            criticals.Add('Ú', "&Uacute;");
            criticals.Add('Û', "&Ucirc;");
            criticals.Add('Ü', "&Uuml;");
            criticals.Add('Ý', "&Yacute;");
            criticals.Add('Þ', "&THORN;");
            criticals.Add('ß', "&szlig;");
            criticals.Add('à', "&agrave;");
            criticals.Add('á', "&aacute;");
            criticals.Add('â', "&acirc;");
            criticals.Add('ã', "&atilde;");
            criticals.Add('ä', "&auml;");
            criticals.Add('å', "&aring;");
            criticals.Add('æ', "&aelig;");
            criticals.Add('ç', "&ccedil;");
            criticals.Add('è', "&egrave;");
            criticals.Add('é', "&eacute;");
            criticals.Add('ê', "&ecirc;");
            criticals.Add('ë', "&euml;");
            criticals.Add('ì', "&igrave;");
            criticals.Add('í', "&iacute;");
            criticals.Add('î', "&icirc;");
            criticals.Add('ï', "&iuml;");
            criticals.Add('ð', "&eth;");
            criticals.Add('ñ', "&ntilde;");
            criticals.Add('ò', "&ograve;");
            criticals.Add('ó', "&oacute;");
            criticals.Add('ô', "&ocirc;");
            criticals.Add('õ', "&otilde;");
            criticals.Add('ö', "&ouml;");
            criticals.Add('÷', "&divide;");
            criticals.Add('ø', "&oslash;");
            criticals.Add('ù', "&ugrave;");
            criticals.Add('ú', "&uacute;");
            criticals.Add('û', "&ucirc;");
            criticals.Add('ü', "&uuml;");
            criticals.Add('ý', "&yacute;");
            criticals.Add('þ', "&thorn;");
            criticals.Add('ÿ', "&yuml;");
        }

        public static String Purge(String text)
        {
            if (criticals.Count == 0)
                CreateCrits();

            StringBuilder htmlk = new StringBuilder();
            int ia = 0;
            for (int i = 0; i < text.Length; i++)
            {
                Char c = text[i];
                if (criticals.ContainsKey(c))
                {
                    htmlk.Append(text.Substring(ia, i - ia));
                    htmlk.Append(criticals[c]);
                    ia = i + 1;
                }
            }
            htmlk.Append(text.Substring(ia));
            return htmlk.ToString();
        }
    }
}
